package com.oplink.webbase.modules.cfg.services;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.oplink.webbase.modules.cfg.dao.CfgNeDao;
import com.oplink.webbase.modules.cfg.dao.CfgSlotDao;
import com.oplink.webbase.modules.cfg.entity.CfgNe;
import com.oplink.webbase.modules.cfg.entity.CfgSlot;

/**
 * 机盘（槽位）
 * 
 * @author yizhang
 */
@Component
@Transactional
public class CfgSlotService {

	@Autowired
	private CfgSlotDao slotDao;

	@Autowired
	private CfgNeDao neDao;

	public List<CfgSlot> getAllSlot() {
		List<CfgSlot> slotlist = slotDao.findAllSlot();
		List<CfgSlot> slist = new ArrayList();
		for (CfgSlot slot : slotlist) {
			if (slot.getCard() != null) {
				if (slot.getCard().getCardtype() != null) {
					slist.add(slot);
				}
			}
		}
		return slist;
	}

	public void save(CfgSlot slot) {
		slotDao.save(slot);
	}

	public long getcard(Long ne, Long slot) {
		List<CfgSlot> slotlist = slotDao.getCard(ne, slot);
		long cardid = 0;
		if (slotlist != null) {
			if (slotlist.size() == 1) {
				CfgSlot sobj = slotlist.get(0);
				if (sobj.getCard() != null) {
					cardid = sobj.getCard().getCardid();
				}
			}
		}
		return cardid;
	}

	public List<Map> getUseCardSlot(String neid) {
		CfgNe ne = neDao.findOne(Long.parseLong(neid));
		List<CfgSlot> slotlist = slotDao.getNotNullSlot(ne.getNid());
		List<Map> list = new ArrayList<Map>();
		Map<String, Object> slotmap = new HashMap<String, Object>();
		for (CfgSlot slot : slotlist) {
			if (slot.getCard() == null) {
				slotlist.remove(slot);
			} else {
				if (slot.getCard().getCardtype() != null) {
					slotmap = new HashMap();
					slotmap.put("cardid", slot.getCard().getCardid());
					slotmap.put("cardtype", slot.getCard().getCardtype().getCardtype());
					slotmap.put("num", slot.getNum());
					list.add(slotmap);
				}
			}
		}
		return list;
	}
}
